package cz.incad.psh.pshassign.server.search;

import com.google.gwt.http.client.URL;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
//import org.apache.commons.httpclient.util.URIUtil;

/**
 *
 * @author alberto
 */
public class SolrSearch implements Search {

    private String q;

    @Override
    public String doSearch(String q) {
        try {
            String urlStr = "http://localhost:8983/solr/select?" + q;
            
            java.net.URL url = new java.net.URL(urlStr);
            
            InputStream is = url.openStream();
            
            Transformer transformer = null;
            TransformerFactory tfactory = TransformerFactory.newInstance();
//            if(xsl!=null){
//                InputStream stylesheet;
//                stylesheet = this.getClass().getResourceAsStream("/cz/incad/psh/pshassign/server/search/"+xsl);
//                StreamSource xslt = new StreamSource(stylesheet);
//                transformer = tfactory.newTransformer(xslt);
//            }else{
                transformer = tfactory.newTransformer();
//            }
            StreamResult destStream = new StreamResult(new StringWriter());
            transformer.transform(new StreamSource(is), destStream);
            StringWriter sw = (StringWriter) destStream.getWriter();
            return sw.toString();
        } catch (Exception e) {
            System.out.println("error :\n" + e);
            e.printStackTrace();
            return "error " + e;
        }
    }
}
